import Vue from "vue";
import App from "./App";
import router from "./router";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import store from "./store";
import http from "./api/index";
import api from "@/utils/api.js";
import filters from "@/filters";

Object.keys(filters).forEach(key => Vue.filter(key, filters[key]));

Vue.config.productionTip = false;
Vue.use(ElementUI);
Vue.prototype.$http = http;
Vue.prototype.$api = api;

//全局前置守卫
router.beforeEach(async (to, from, next) => {
  if (to.name == "Login") {
    next();
  } else {
    var token = localStorage.getItem("_token");
    if (!token) {
      ElementUI.Message("请先输入姓名");
      next("/login");
    } else {
      //获得权限
      await store.dispatch("getRole");
      next();
    }
  }
});

/* eslint-disable no-new */
new Vue({
  el: "#app",
  router,
  store,
  render: h => h(App)
});
